package androidtool.md5key;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hoang
 */
public class MyKey {

    private String allInformation;
    private String mD5;
    private String sHA1;
    private String sHA256;

    public MyKey() {
        setAllInformation();
        System.out.println(allInformation);
        mD5 = filterMD5(allInformation);
        sHA1 = filterSHA1(allInformation);
        sHA256 = filterSHA256(allInformation);
    }

    public String getMD5() {
        return mD5;
    }

    public String getSHA1() {
        return sHA1;
    }

    public String getsHA256() {
        return sHA256;
    }

    private String getDirKeyTool() {
        String dirJava;
        dirJava = "C:\\Program Files\\Java";
        File dir = new File(dirJava);
        String[] child = dir.list();
        if (child == null) {
            return "";
        } else {
            return (dirJava + "\\" + child[0] + "\\bin\\keytool.exe");
        }
    }

    private String getCommand() {
        String dirKeyTool = getDirKeyTool();
        int versionJdk = 0;
        for (int i = 0; i < dirKeyTool.length() - 3; i++) {
            if (dirKeyTool.substring(i, i + 3).equalsIgnoreCase("jdk")) {
                versionJdk = Integer.parseInt(dirKeyTool.substring(i + 5, i + 6));
                break;
            }
        }

        if (versionJdk <= 6) {
            return dirKeyTool + " -list -alias androiddebugkey -keystore C:\\Users\\"
                    + System.getProperty("user.name")
                    + "\\.android\\debug.keystore";
        } else {
            return dirKeyTool + " -v -list -alias androiddebugkey -keystore C:\\Users\\"
                    + System.getProperty("user.name")
                    + "\\.android\\debug.keystore -storepass android -keypass android";
        }
    }

    private String filterMD5(String s) {
        for (int i = 0; i < s.length() - 4; i++) {
            if (s.substring(i, i + 4).equalsIgnoreCase("MD5:")) {
                String md5 = s.substring(i + 6, i + 53);
                System.out.println("md5= begin_" + md5 + "_end");
                return md5;
            }
        }
        return "";
    }

    private String filterSHA1(String s) {
        for (int i = 0; i < s.length() - 4; i++) {
            if (s.substring(i, i + 5).equalsIgnoreCase("SHA1:")) {
                String sha1 = s.substring(i + 6, i + 65);
                System.out.println("sha1= begin_" + sha1 + "_end");
                return sha1;
            }
        }
        return "";
    }
    private String filterSHA256(String s) {
        for (int i = 0; i < s.length() - 4; i++) {
            if (s.substring(i, i + 7).equalsIgnoreCase("SHA256:")) {
                String sha1 = s.substring(i + 8, i + 103);
                System.out.println("sha256= begin_" + sha1 + "_end");
                return sha1;
            }
        }
        return "";
    }

    private void setAllInformation() {
        allInformation = "";
        try {
            String command = getCommand();
            Process p = Runtime.getRuntime().exec(command);
            InputStream in = p.getInputStream();
            int c;
            while ((c = in.read()) != -1) {
                allInformation += (char) c;
            }
            in.close();
            p.destroy();
        } catch (IOException ex) {
            Logger.getLogger(MyKey.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
